package net.oschina.app.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import net.oschina.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 自定义dialog
 */

public class MyDialog extends Dialog{
    @BindView(R.id.ll_photo)
    LinearLayout mLlPhoto;
    @BindView(R.id.ll_tack_picture)
    LinearLayout mLlTackPicture;
    @BindView(R.id.ll_write)
    LinearLayout mLlWrite;
    private Context mContext;

    public MyDialog(Context context) {
        super(context, R.style.Theme_Dialog_Address);
        this.mContext = context;
    }

    /**
     * 初始化自定义Dialog
     */
    private void initDialog() {
        Window window = getWindow();   //  获取窗口对象
        WindowManager.LayoutParams params = window.getAttributes();  //  获取params
        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; //  设置底部居中显示
        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); // 获取屏幕的宽高
        params.width = metrics.widthPixels;                  // 设置dialog的宽为屏幕的宽
        params.height = (int) (metrics.heightPixels * 0.3);  // 设置dialog的高为屏幕高的1/3
        window.setAttributes(params);                        // 将params设置给window
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_view);  //  dialog内容的布局
        ButterKnife.bind(this);                //  绑定 ButterKnife
        initDialog();   //  初始化自定义Dialog
    }

    @OnClick({R.id.ll_photo, R.id.ll_tack_picture, R.id.ll_write, R.id.iv_close})
    public void onClick(View v) {
        if (onClickItemListening == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.ll_photo:
                onClickItemListening.clickPhoto();
                break;
            case R.id.ll_tack_picture:
                onClickItemListening.clickTackPicture();
                break;
            case R.id.ll_write:
                onClickItemListening.clickWrite();
                break;
            case R.id.iv_close :
                onClickItemListening.clickClose();
                ButterKnife.bind(this).unbind();   //  解绑 ButterKnife
                break;
        }
    }

    private OnClickItemListening onClickItemListening;
    public void setOnClickItemListening(OnClickItemListening onClickItemListening){
        this.onClickItemListening = onClickItemListening;
    }
    public interface OnClickItemListening{
        void clickWrite();
        void clickPhoto();
        void clickTackPicture();
        void clickClose();
    }
}
